package com.fengzhi.crawler._51job;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class _51JobCrawler {

	// static String link = "http://jobs.51job.com/foshan/";
	static String link= "http://jobs.51job.com/nanjing/";

	public boolean start() throws IOException {
		if (!LinkQueue.isUnVisitedURLsEmpty()) {
			String url = LinkQueue.unVisitedURLDeQueue();
			LinkQueue.addVisitedURL(url);

			Document doc = Jsoup.connect(url).timeout(5 * 1000).get();

			doc = denoiseForDoc(doc);
			doc = areaParser(doc);

			fetchLinkAddToQueue(doc);

			if (!LinkQueue.isUnVisitedURLsEmpty()) {
				return true;
			}
		}

		return false;
	}

	public void fetchLinkAddToQueue(Document doc) {
		Element element = doc.getElementById("cppageno");

		Elements aEles = element.getElementsByTag("a");

		for (Element e : aEles) {
			LinkQueue.addUnVisitedURL(e.attr("href"));
		}
	}

	public Document areaParser(Document doc) {
		doc.getElementById("top").remove();
		doc.getElementById("bottom").remove();
		doc.getElementsByClass("right").remove();
		doc.getElementsByClass("opat").remove();

		Elements companyEles = doc.body().select(".mcon");

		for (Element ele : companyEles) {
			Elements infoEles = ele.getElementsByClass("e");

			for (Element infoEle : infoEles) {
				Elements eInfo = infoEle.getElementsByClass("info");

				String job = eInfo.select("a").first().text();
				if (!check(job)) {
					continue;
				}

				Elements eOrder = infoEle.getElementsByClass("order");
				Elements eText = infoEle.getElementsByClass("text");

				_51JobEntity entity = new _51JobEntity();

				entity.setJobInfo(eOrder.text());
				entity.setJobRequirements(eText.text());

				entity.setJob(eInfo.select("a").first().text());
				entity.setJobUrl(eInfo.select("a").first().attr("href"));

				for (Element e : eInfo) {
					entity.setMoney(e.getElementsByClass("location").text());
					entity.setTime(e.getElementsByClass("time").text());
					entity.setAddress(e.getElementsByClass("name").text());
				}

				System.out.println("***************************************************************");
				System.out.println("工作：" + entity.getJob());
				System.out.println("工作详情地址：" + entity.getJobUrl());
				System.out.println("要求：" + entity.getJobRequirements());
				System.out.println("工作地址：" + entity.getAddress());
				System.out.println("工作信息：" + entity.getJobInfo());
				System.out.println("工资：" + entity.getMoney());
				System.out.println("发布时间：" + entity.getTime());
				System.out.println("***************************************************************");

			}
		}

		return doc;
	}

	public boolean check(String job) {

		Set<String> javaSet = new HashSet<String>() {
			{
				add("Java");
				add("java");
				add("软件开发");
				add("软件工程师");
				add("java程序员");
				add("java中级工程师");
				add("java高级工程师");
				add("Java程序员");
				add("Java中级工程师");
				add("Java高级工程师");
			}
		};

		if (job != null && !"".equals(job.trim())) {
			if (job.indexOf("java") != -1 || job.indexOf("软件开发") != -1) {
				return true;
			}
		}

		return false;
	}

	public Document denoiseForDoc(Document document) {
		document.getElementsByTag("script").remove();
		document.getElementsByTag("style").remove();
		document.getElementsByTag("select").remove();
		document.getElementsByTag("link").remove();
		document.getElementsByTag("input").remove();
		document.getElementsByTag("object").remove();
		document.getElementsByTag("textarea").remove();
		document.getElementsByAttributeValue("display", "none").remove();
		document.getElementsByAttributeValueContaining("style", "display:none").remove();
		document.getElementsByAttributeValueContaining("style", "overflow: hidden").remove();

		return document;
	}

	public static void main(String[] args) {
		_51JobCrawler jobCrawler = new _51JobCrawler();

		try {
			LinkQueue.addUnVisitedURL(link);

			boolean flag = true;
			while (flag) {
				flag = jobCrawler.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
